<?php
// Copyright 2009 Oy Site Logic Ab
// 
// This file is part of a program, that is distributed
// under the terms of the GNU General Public License,
// see license.txt for details.

defined( '_JEXEC' ) or die( 'Restricted access' );

require_once(JPATH_SITE .DS.'/components/com_palvelutiski/configuration.php');
require_once(JPATH_SITE .DS.'/components/com_palvelutiski/database.php');

//require_once JPATH_ADMINISTRATOR.DS.'components'.DS.'com_eventcalendar'.DS.'helpers'.DS.'database.php';

$task = JRequest::getCmd('task', '');

switch ($task) {
	case 'getPosition':
		getPosition();
		break;
	case 'create':
		createPosition();
		break;
	default:
    
}

function createPosition() {
	global $ptdb, $palvelutiski_siteId;
	
	//Ajax stuff is sent as utf8
//	$name = utf8_decode(JRequest::getVar('name'));
	$name = JRequest::getVar('name');
//	$address = utf8_decode(JRequest::getVar('address'));
	$address = JRequest::getVar('address');
	$zoom = (int)JRequest::getVar('zoom');
	$lon = (float)JRequest::getVar('lon');
	$lat = (float)JRequest::getVar('lat');
	
	$query = 'INSERT INTO @__dpposition SET';
	$query .= ' dpposition_lon='.$lon;
	$query .= ',dpposition_lat='.$lat;
	$query .= ',dpposition_name='.$ptdb->Quote($name);
	$query .= ',dpposition_street='.$ptdb->Quote($address);
	$query .= ',dpposition_zoom='.$zoom;
	$query .= ',dpposition_status=0';
	$query .= ',dpjoomla_id='.$palvelutiski_siteId;
	
	$ptdb->setQuery($query);
	$ptdb->query();
	
	$id = $ptdb->insertid();
	
	//Add the right stuff to the tags table as well
	if ($id) {
		$query = 'INSERT INTO @__dpposition_has_tag';
		$query .= " VALUES ($id, (SELECT dpjoomla_position_dptag_id FROM @__dpjoomla WHERE dpjoomla_id = $palvelutiski_siteId))";
		$ptdb->setQuery($query);
		$ptdb->query();
	}
	
//	if (!$id) {
//		print_r($ptdb);
//	}
	echo $id;
}

function getPosition() {



	global $ptdb, $palvelutiski_siteId;
	$result = new stdClass;
	
	$id = (int) JRequest::getCmd('id');
	$position = null;
	
	if ($id) {
		$query = 'SELECT * FROM @__dpposition';
		$query .= ' WHERE dpposition_id = '.$id;
		$ptdb->setQuery($query);
		$position = $ptdb->loadObject();
		if ($position) {
			$result->position = $position;
//			$result->position->dpposition_name = utf8_encode($result->position->dpposition_name);
			$result->position->dpposition_name = $result->position->dpposition_name;
//			$result->position->dpposition_street = utf8_encode($result->position->dpposition_street);
			$result->position->dpposition_street = $result->position->dpposition_street;
		}
	//print_r($ptdb);
	}


	

	if (!$position) {
		//Load default position from database
		$query = 'SELECT dpjoomla_position_lat AS dpposition_lat, dpjoomla_position_lon AS dpposition_lon FROM @__dpjoomla WHERE dpjoomla_id = '.$palvelutiski_siteId;
		$ptdb->setQuery($query);
		$result->position = $ptdb->loadObject();
		if (!$result->position) {
			$result->position = new stdClass;
			$result->position->dpposition_lon = 24.937505722;
			$result->position->dpposition_lat = 60.169610135;
		}
		$result->position->dpposition_zoom = 15;
		$result->position->isDefault = true;
	}


	$query = 'SELECT p.* FROM @__dpposition AS p, @__dpposition_has_tag AS t, @__dpjoomla AS j';
	$query .= ' WHERE p.dpposition_status = 1';
	$query .= ' AND p.dpposition_lon IS NOT NULL';
	$query .= ' AND p.dpposition_lat IS NOT NULL';
	$query .= ' AND p.dpposition_id = t.dpposition_id';
	$query .= ' AND t.dptag_id = j.dpjoomla_position_dptag_id';
	$query .= ' AND j.dpjoomla_id = '.$palvelutiski_siteId;
	//TODO probably some filtering needed
	$ptdb->setQuery($query);
	
	$result->list = $ptdb->loadObjectList();
	
	foreach($result->list as $entry) {
//		$entry->dpposition_name = utf8_encode($entry->dpposition_name);	
		$entry->dpposition_name = $entry->dpposition_name;
//		$entry->dpposition_street = utf8_encode($entry->dpposition_street);
		$entry->dpposition_street = $entry->dpposition_street;
	}

	
	echo json_encode($result);
	
	//TMP-content
	//echo '{"position":{"dpposition_lat":"60.229806000","dpposition_lon":"24.930590000","dpposition_zoom":15,"isDefault":true},"list":[{"dpposition_id":"35","dpposition_lon":"24.930034866","dpposition_lat":"60.229756619","dpposition_zoom":"2","dpposition_name":"Maunulan kirkko","dpposition_details":"","dpposition_street":"Mets\u00e4purontie 15","dpposition_zip":"","dpposition_status":"1","dpjoomla_id":"7"},{"dpposition_id":"38","dpposition_lon":"24.927381805","dpposition_lat":"60.228554095","dpposition_zoom":"2","dpposition_name":"Maunulan Mediapaja","dpposition_details":"","dpposition_street":"Mets\u00e4purontie 19-21","dpposition_zip":"","dpposition_status":"1","dpjoomla_id":"7"},{"dpposition_id":"36","dpposition_lon":"24.925992181","dpposition_lat":"60.227907939","dpposition_zoom":"2","dpposition_name":"Palvelukeskus Saunabaari","dpposition_details":"Tilat ovat toisessa ja kolmannessa kerroksessa","dpposition_street":"Mets\u00e4purontie 25","dpposition_zip":"","dpposition_status":"1","dpjoomla_id":"7"},{"dpposition_id":"39","dpposition_lon":"24.932905034","dpposition_lat":"60.231874549","dpposition_zoom":"2","dpposition_name":"Maunulan apteekki","dpposition_details":"","dpposition_street":"Pakilantie 10","dpposition_zip":"","dpposition_status":"1","dpjoomla_id":"7"},{"dpposition_id":"87","dpposition_lon":"24.926944100","dpposition_lat":"60.228772800","dpposition_zoom":"15","dpposition_name":"Maunula","dpposition_details":"","dpposition_street":"Mets\u00e4purontie 20","dpposition_zip":null,"dpposition_status":"1","dpjoomla_id":"7"},{"dpposition_id":"92","dpposition_lon":"24.919996262","dpposition_lat":"60.230607445","dpposition_zoom":"15","dpposition_name":"Tertun taimi ja kukka","dpposition_details":"","dpposition_street":"Pirkkolantie 20","dpposition_zip":null,"dpposition_status":"1","dpjoomla_id":"7"},{"dpposition_id":"100","dpposition_lon":"24.932972575","dpposition_lat":"60.230999513","dpposition_zoom":"2","dpposition_name":"S-Market Maunula","dpposition_details":"","dpposition_street":"Mets\u00e4purontie 1","dpposition_zip":null,"dpposition_status":"1","dpjoomla_id":"7"},{"dpposition_id":"101","dpposition_lon":"24.927052581","dpposition_lat":"60.228109896","dpposition_zoom":"2","dpposition_name":"Alepa Maunula","dpposition_details":"","dpposition_street":"Mets\u00e4purontie 23","dpposition_zip":null,"dpposition_status":"1","dpjoomla_id":"7"},{"dpposition_id":"103","dpposition_lon":"24.931926481","dpposition_lat":"60.232794666","dpposition_zoom":"2","dpposition_name":"JR-Rauta Oy","dpposition_details":"","dpposition_street":"Pakilantie 14","dpposition_zip":null,"dpposition_status":"1","dpjoomla_id":"7"},{"dpposition_id":"104","dpposition_lon":"24.923046561","dpposition_lat":"60.228846010","dpposition_zoom":"2","dpposition_name":"DeeSport","dpposition_details":"","dpposition_street":"Mets\u00e4purontie 29","dpposition_zip":null,"dpposition_status":"1","dpjoomla_id":"7"},{"dpposition_id":"105","dpposition_lon":"24.963793502","dpposition_lat":"60.228515840","dpposition_zoom":"2","dpposition_name":"Hiusatelje","dpposition_details":"","dpposition_street":"Kyl\u00e4nvanhimmantie 29","dpposition_zip":null,"dpposition_status":"1","dpjoomla_id":"7"},{"dpposition_id":"107","dpposition_lon":"24.927269175","dpposition_lat":"60.228343243","dpposition_zoom":"2","dpposition_name":"Sami-Talo","dpposition_details":"","dpposition_street":"Mets\u00e4purontie 19-21","dpposition_zip":null,"dpposition_status":"1","dpjoomla_id":"7"},{"dpposition_id":"108","dpposition_lon":"24.963793502","dpposition_lat":"60.228515840","dpposition_zoom":"2","dpposition_name":"Hiusatelje","dpposition_details":"","dpposition_street":"Kyl\u00e4nvanhimmantie 29","dpposition_zip":null,"dpposition_status":"1","dpjoomla_id":"7"},{"dpposition_id":"110","dpposition_lon":"24.963793502","dpposition_lat":"60.228515840","dpposition_zoom":"2","dpposition_name":"Kauppakeskus Ogeli","dpposition_details":"","dpposition_street":"Kyl\u00e4nvanhimmantie 29","dpposition_zip":null,"dpposition_status":"1","dpjoomla_id":"7"},{"dpposition_id":"112","dpposition_lon":"24.930878422","dpposition_lat":"60.228791772","dpposition_zoom":"2","dpposition_name":"Maunulan liikuntahalli","dpposition_details":"","dpposition_street":"Maunulanm\u00e4ki 3-5","dpposition_zip":null,"dpposition_status":"1","dpjoomla_id":"7"},{"dpposition_id":"70","dpposition_lon":"24.933555628","dpposition_lat":"60.234082407","dpposition_zoom":"1","dpposition_name":"Maunulan kirjasto","dpposition_details":"","dpposition_street":"Suursuonlaita 6","dpposition_zip":"","dpposition_status":"1","dpjoomla_id":"7"},{"dpposition_id":"77","dpposition_lon":"24.934168574","dpposition_lat":"60.232394991","dpposition_zoom":"0","dpposition_name":"Maunulan ostoskeskus","dpposition_details":"","dpposition_street":"Pakilantie 11","dpposition_zip":"","dpposition_status":"1","dpjoomla_id":"7"},{"dpposition_id":"148","dpposition_lon":"24.926218987","dpposition_lat":"60.226665244","dpposition_zoom":"15","dpposition_name":"Verhoomo Jouhi","dpposition_details":"","dpposition_street":"T\u00f6yrytie 8","dpposition_zip":null,"dpposition_status":"1","dpjoomla_id":"7"},{"dpposition_id":"150","dpposition_lon":"24.940381050","dpposition_lat":"60.220612511","dpposition_zoom":"15","dpposition_name":"Mets\u00e4l\u00e4n autohuolto Oy","dpposition_details":"","dpposition_street":"Asesep\u00e4nkuja 2","dpposition_zip":null,"dpposition_status":"1","dpjoomla_id":"7"},{"dpposition_id":"151","dpposition_lon":"24.936304092","dpposition_lat":"60.234506561","dpposition_zoom":"15","dpposition_name":"Arcobaleno","dpposition_details":"","dpposition_street":"Suursuonlaita 1","dpposition_zip":null,"dpposition_status":"1","dpjoomla_id":"7"},{"dpposition_id":"152","dpposition_lon":"24.926304817","dpposition_lat":"60.226580002","dpposition_zoom":"15","dpposition_name":"Kirsma","dpposition_details":"","dpposition_street":"T\u00f6yrytie 8","dpposition_zip":null,"dpposition_status":"1","dpjoomla_id":"7"},{"dpposition_id":"153","dpposition_lon":"24.941797257","dpposition_lat":"60.240386317","dpposition_zoom":"15","dpposition_name":"Asustehuolto UllaStiina","dpposition_details":"","dpposition_street":"Lepolantie 16 A","dpposition_zip":null,"dpposition_status":"1","dpjoomla_id":"7"},{"dpposition_id":"154","dpposition_lon":"24.926819801","dpposition_lat":"60.227283242","dpposition_zoom":"15","dpposition_name":"Parturikampaamo Tukkakulma","dpposition_details":"","dpposition_street":"M\u00e4nnikk\u00f6tie 10","dpposition_zip":null,"dpposition_status":"1","dpjoomla_id":"7"},{"dpposition_id":"230","dpposition_lon":"24.924501400","dpposition_lat":"60.228505100","dpposition_zoom":"15","dpposition_name":"Parturi-Kampaamo Fiveboys","dpposition_details":"","dpposition_street":"Mets\u00e4purontie 26","dpposition_zip":null,"dpposition_status":"1","dpjoomla_id":"7"},{"dpposition_id":"241","dpposition_lon":"24.926261902","dpposition_lat":"60.228625749","dpposition_zoom":"15","dpposition_name":"Maunula","dpposition_details":"","dpposition_street":"Mets\u00e4purontie 18-22","dpposition_zip":null,"dpposition_status":"1","dpjoomla_id":"7"},{"dpposition_id":"242","dpposition_lon":"24.924501400","dpposition_lat":"60.228505100","dpposition_zoom":"15","dpposition_name":"Mma Kru","dpposition_details":"","dpposition_street":"Mets\u00e4purontie 26 E","dpposition_zip":null,"dpposition_status":"1","dpjoomla_id":"7"},{"dpposition_id":"260","dpposition_lon":"24.926261902","dpposition_lat":"60.228625749","dpposition_zoom":"15","dpposition_name":"Maunula","dpposition_details":"","dpposition_street":"Mets\u00e4purontie 20","dpposition_zip":null,"dpposition_status":"1","dpjoomla_id":"7"},{"dpposition_id":"286","dpposition_lon":"24.962563600","dpposition_lat":"60.220486700","dpposition_zoom":"15","dpposition_name":"Cafe Oulunkyl\u00e4, Kahvila","dpposition_details":"","dpposition_street":"Oulunkyl\u00e4ntie 7","dpposition_zip":null,"dpposition_status":"1","dpjoomla_id":"7"},{"dpposition_id":"368","dpposition_lon":"24.966352800","dpposition_lat":"60.230542500","dpposition_zoom":"15","dpposition_name":"Oiva Olo - Klassista hierontaa","dpposition_details":"","dpposition_street":"Siltavoudintie 5, 00640 Helsinki","dpposition_zip":null,"dpposition_status":"1","dpjoomla_id":"7"},{"dpposition_id":"384","dpposition_lon":"24.930682182","dpposition_lat":"60.225535769","dpposition_zoom":"15","dpposition_name":"KehoSalonki","dpposition_details":"","dpposition_street":"Koivikkotie 8 \/ M\u00e4nnikk\u00f6tie 3","dpposition_zip":null,"dpposition_status":"1","dpjoomla_id":"7"}]}';
}

?>
